package com.mhl.view;

import com.mhl.domain.*;
import com.mhl.service.BillService;
import com.mhl.service.DiningTableService;
import com.mhl.service.EmployeeService;
import com.mhl.service.MenuService;
import com.mhl.utils.Utility;

import java.util.List;

/**
 * 主界面
 */
public class MHLView {

    private boolean loop = true; // 控制是否退出菜单
    private String key; // 接收用户的输入

    // 定义EmployService对象
    private EmployeeService employeeService = new EmployeeService();

    // 定义DiningTableService的对象
    private DiningTableService diningTableService = new DiningTableService();

    // 定义MenuService的对象
    private MenuService menuService = new MenuService();

    // 定义BillService属性
    private BillService billService = new BillService();

    // 完成结账
    public void payBill() {
        System.out.println("=======================结账=======================");
        System.out.print("请输入的餐桌编号（-1 取消）：");
        int diningTableId = Utility.readInt();
        if (diningTableId == -1) {
            System.out.println("=======================取消结账=======================");
            return;
        }

        // 验证餐桌是否存在
        DiningTable diningTable = diningTableService.getDiningTableById(diningTableId);
        if (diningTable == null) {
            System.out.println("=======================结账的餐桌不存在=======================");
            return;
        }

        // 验证餐桌是否有需要结账的账单
        if (!billService.hasPayBillDiningTableId(diningTableId)) {
            System.out.println("=======================没有需要结账的账单=======================");
            return;
        }
        System.out.print("结账方式（现金/支付宝/微信）（-1 取消）：");
        String payMode = Utility.readString(20, ""); //如果直接回车，返回空串
        if ("".equals(payMode)) {
            System.out.println("==========================取消结账==========================");
            return;
        }
        char key = Utility.readConfirmSelection();
        if (key == 'Y') {
            if (billService.payBill(diningTableId, payMode)) {
                System.out.println("==========================结账成功==========================");
            } else {
                System.out.println("==========================结账失败==========================");
            }
        } else {
            System.out.println("==========================取消结账==========================");
        }
    }

    // 显示账单信息
    public void listBill() {
//        List<Bill> bills = billService.list();
//        System.out.println("\n编号\t\t菜品号\t\t菜品量\t\t金额\t\t桌号\t\t日期\t\t\t\t\t\t\t状态");
//        for (Bill bill : bills) {
//            System.out.println(bill);
//        }
//        System.out.println("=======================显示完毕=======================");

        List<MultiTableBean> multiTableBeanList = billService.list2();
        System.out.println("\n编号\t\t菜品号\t\t菜品量\t\t金额\t\t桌号\t\t日期\t\t\t\t\t\t\t状态\t\t菜品名");
        for (MultiTableBean multiTableBean : multiTableBeanList) {
            System.out.println(multiTableBean);
        }
        System.out.println("=======================显示完毕=======================");
    }

    // 完成点餐
    public void orderMenu() {
        System.out.println("=======================点餐服务=======================");
        System.out.print("请输入的餐桌编号（-1 取消）：");
        int orderDingTableId = Utility.readInt();
        if (orderDingTableId == -1) {
            System.out.println("=======================取消点餐=======================");
            return;
        }
        System.out.print("请输入的点餐的菜品号（-1 取消）：");
        int orderMenuId = Utility.readInt();
        if (orderMenuId == -1) {
            System.out.println("=======================取消点餐=======================");
            return;
        }
        System.out.print("请输入的点餐的菜品量（-1 取消）：");
        int orderNums = Utility.readInt();
        if (orderNums == -1) {
            System.out.println("=======================取消点餐=======================");
            return;
        }

        // 验证餐桌号是否存在
        DiningTable diningTable = diningTableService.getDiningTableById(orderDingTableId);
        if (diningTable == null) {
            System.out.println("=======================餐桌号不存在=======================");
            return;
        }

        // 验证菜品编号
        Menu menuById = menuService.getMenuById(orderMenuId);
        if (menuById == null) {
            System.out.println("=======================菜品号不存在=======================");
            return;
        }

        // 点餐
        if (billService.orderMenu(orderMenuId, orderNums, orderDingTableId)) {
            System.out.println("=======================点餐成功=======================");
        } else {
            System.out.println("=======================点餐失败=======================");
        }
    }

    public static void main(String[] args) {
        new MHLView().mainMenu();
    }

    // 显示所有菜品
    public void listMenu() {
        List<Menu> lists = menuService.list();
        System.out.println("\n菜品编号\t\t菜品名\t\t类别\t\t价格");
        for (Menu list : lists) {
            System.out.println(list);
        }
        System.out.println("=======================显示完毕=======================");
    }

    // 完成订座
    public void orderDiningTable() {
        System.out.println("=======================预定餐桌=======================");
        System.out.print("请选择要预定的餐桌编号（-1 取消）：");
        int orderId = Utility.readInt();
        if (orderId == -1) {
            System.out.println("======================取消预订餐桌======================");
            return;
        }
        // 该方法得到的是Y或N
        char key = Utility.readConfirmSelection();
        if (key == 'Y') { // 要预定
            // 根据orderId 返回对应DiningTable对象 如果为null 说明对象不存在
            DiningTable diningTable = diningTableService.getDiningTableById(orderId);
            if (diningTable == null) {
                System.out.println("=======================预订餐桌不存在=======================");
                return;
            }

            // 判断该餐桌的状态是否为空状态
            if (!("空".equals(diningTable.getState()))) { // 说明当前餐桌不是空状态
                System.out.println("=======================该餐桌已被预定或就餐中=======================");
                return;
            }

            // 这时说明可以预定 接收预定信息
            System.out.print("预定人名字：");
            String orderName = Utility.readString(50);
            System.out.print("预定人电话：");
            String orderTel = Utility.readString(50);

            // 更新餐桌状态
            if (diningTableService.orderDiningTable(orderId, orderName, orderTel)) {
                System.out.println("=======================预订餐桌成功=======================");
            } else {
                System.out.println("=======================预订餐桌失败=======================");
            }

        } else {
            System.out.println("=======================取消预订餐桌=======================");
        }

    }

    // 显示所有餐桌状态
    public void listDiningTable() {
        List<DiningTable> lists = diningTableService.list();
        System.out.println("\n餐桌编号\t\t餐桌状态");
        for (DiningTable diningTable : lists) {
            System.out.println(diningTable);
        }
        System.out.println("=======================显示完毕=======================");
    }

    // 显示主菜单
    public void mainMenu() {
        while (loop) {
            System.out.println("================================满汉楼================================");
            System.out.println("\t\t\t\t\t\t\t1 登录满汉楼");
            System.out.println("\t\t\t\t\t\t\t2 退出满汉楼");
            System.out.print("please enter your choice: ");
            key = Utility.readString(1);
            switch (key) {
                case "1":
                    System.out.print("请输入员工号： ");
                    String empId = Utility.readString(50);
                    System.out.print("请输入密码： ");
                    String pwd = Utility.readString(50);
                    Employee employee = employeeService.getEmployeeByIdAndPwd(empId, pwd);
                    if (employee != null) { // 说明该用户存在
                        System.out.println("=======================登陆成功[" + employee.getName() + "]=======================\n");
                        // 显示二级菜单
                        while (loop) {
                            System.out.println("=======================满汉楼二级菜单=======================");
                            System.out.println("\t\t\t\t\t1 显示餐桌状态");
                            System.out.println("\t\t\t\t\t2 预定餐桌");
                            System.out.println("\t\t\t\t\t3 显示所有菜品");
                            System.out.println("\t\t\t\t\t4 点餐服务");
                            System.out.println("\t\t\t\t\t5 查看账单");
                            System.out.println("\t\t\t\t\t6 结账");
                            System.out.println("\t\t\t\t\t9 退出满汉楼");
                            System.out.print("please enter your choice: ");
                            key = Utility.readString(1);
                            switch (key) {
                                case "1":
                                    listDiningTable();
                                    break;
                                case "2":
                                    orderDiningTable();
                                    break;
                                case "3":
                                    listMenu();
                                    break;
                                case "4":
                                    orderMenu();
                                    break;
                                case "5":
                                    listBill();
                                    break;
                                case "6":
                                    payBill();
                                    break;
                                case "9":
                                    loop = false;
                                    break;
                                default:
                                    System.out.println("选择有误，请重新选择。");
                            }

                        }
                    } else {
                        System.out.println("=======================登陆失败=======================");
                    }
                    break;
                case "2":
                    System.out.println("退出满汉楼。");
                    loop = false;
                    break;
                default:
                    System.out.println("输入有误，请重新输入。");

            }
        }
    }


}
